//
//  ST_FilterFeedViewController.swift
//  btvideo
//
//  Created by mac on 2020/6/3.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import RealmSwift
import SwifterSwift


/**
 只看真人认证
 */
var OnlyLookFaceDetected = false



/**
 只看附近
 */
var OnlyLookNeighbor = false

class ST_FilterFeedViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(16)
            make.height.equalTo(296+50+BTSafeAreaBottom)
        }
        
        view.addSubview(spaceBtn)
        spaceBtn.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(22)
        }
        
        root_view.addSubview(item0)
        item0.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.height.equalTo(50)
        }
        
        root_view.addSubview(item1)
        item1.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(item0.snp.bottom)
            make.height.equalTo(50)
        }
        
        root_view.addSubview(item3)
        item3.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(item1.snp.bottom)
            make.height.equalTo(50)
        }
        
        root_view.addSubview(item2)
        item2.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(item3.snp.bottom)
            make.height.equalTo(50)
        }
        
        
        
        
        
        item0.click_closure = {[weak self] (v:UIView , f:Bool) in
            
            
            self?.item0.model?.isSelected = f
            self?.item0.model = self?.item0.model
            
            
            self?.item1.model?.isSelected = !f
            self?.item1.model = self?.item1.model
        }
        
        item1.click_closure = {[weak self] (v:UIView , f:Bool) in
            
            
            self?.item1.model?.isSelected = f
            self?.item1.model = self?.item1.model
            
            
            self?.item0.model?.isSelected = !f
            self?.item0.model = self?.item0.model
        }
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(260)
            make.height.equalTo(44)
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom-20)
        }
        
        if let selected_sex = normal_selected_sex{
            
            if selected_sex == .male{
                item0.model = ST_FilterFeedItemViewModel.init(str: "女生的特质", isSelected: false)
                item1.model = ST_FilterFeedItemViewModel.init(str: "男生的特质", isSelected: true)
            }else{
                item0.model = ST_FilterFeedItemViewModel.init(str: "女生的特质", isSelected: true)
                item1.model = ST_FilterFeedItemViewModel.init(str: "男生的特质", isSelected: false)
            }
        }
        
        
    }
    
//    MARK: - Properties
    var normal_selected_sex:ST_SEX?
    
    
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "对你显示"
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var spaceBtn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = .clear
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var confirm_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("确定")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 22
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var item0:ST_FilterFeedItemView = {
       let control = ST_FilterFeedItemView()
        
        return control
    }()
    
    fileprivate lazy var item1:ST_FilterFeedItemView = {
       let control = ST_FilterFeedItemView()
       return control
    }()
    
    fileprivate lazy var item2:ST_FilterFeedSwitchItemView = {
       let control = ST_FilterFeedSwitchItemView()
        control.text = "只看通过真人认证的特质"
        control.select_control.isOn = OnlyLookFaceDetected
       return control
    }()
    
    fileprivate lazy var item3:ST_FilterFeedSwitchItemView = {
       let control = ST_FilterFeedSwitchItemView()
        control.text = "只看附近的特质"
        control.select_control.isOn = OnlyLookNeighbor
       return control
    }()
    
    var confirm_closure:((ST_SEX)->())?
    
    @objc func confirm_handle(){
        
        
        
        OnlyLookFaceDetected = item2.select_control.isOn
        
        OnlyLookNeighbor = item3.select_control.isOn
        
        if item0.model?.isSelected == true{
            
            confirm_closure?(ST_SEX.female)
        }
        
        if item1.model?.isSelected == true{
            
            confirm_closure?(ST_SEX.male)
        }
        
        goback_previous_controller()
    }
    
    @objc func close_handle(){
        goback_previous_controller()
    }
    
}

//MARK: - 筛选日常 男女 等
class ST_FilterGenderOfDailyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear
        
        view.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(16)
            make.height.equalTo(296+BTSafeAreaBottom)
        }
        
        view.addSubview(spaceBtn)
        spaceBtn.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(root_view.snp.top)
        }
        
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(22)
        }
        
        root_view.addSubview(item0)
        item0.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.height.equalTo(50)
        }
        
        root_view.addSubview(item1)
        item1.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(item0.snp.bottom)
            make.height.equalTo(50)
        }
        
        root_view.addSubview(item2)
        item2.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(item1.snp.bottom)
            make.height.equalTo(50)
        }
        
        item0.click_closure = {[weak self] (v:ST_FilterFeedItemView , f:Bool) in
            
            v.model?.isSelected = f
            
            v.refresh_bind_model()
            
            
            self?.last_selected_item.model?.isSelected = !f
            self?.last_selected_item.model = self?.last_selected_item.model
            self?.last_selected_item = v
        }
        
        item1.click_closure = {[weak self] (v:ST_FilterFeedItemView , f:Bool) in
            
            
            v.model?.isSelected = f
            
            v.refresh_bind_model()
            
            
            self?.last_selected_item.model?.isSelected = !f
            self?.last_selected_item.model = self?.last_selected_item.model
            self?.last_selected_item = v
        }
        
        item2.click_closure = {[weak self] (v:ST_FilterFeedItemView , f:Bool) in
            
            
            v.model?.isSelected = f
            
            v.refresh_bind_model()
            
            
            self?.last_selected_item.model?.isSelected = !f
            self?.last_selected_item.model = self?.last_selected_item.model
            self?.last_selected_item = v
        }
        
        root_view.addSubview(confirm_btn)
        confirm_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.width.equalTo(260)
            make.height.equalTo(44)
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom-20)
        }
        
    }
    
//    MARK: - Properties
    fileprivate lazy var root_view:UIView = {
        let control = UIView()
        control.backgroundColor = Color.white
        control.cornerRadius = 16
        return control
    }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "对你显示"
        control.textColor = Color.init(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var spaceBtn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = .clear
        control.addTarget(self, action: #selector(close_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    fileprivate lazy var confirm_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setTitleForAllStates("确定")
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.setTitleColorForAllStates(UIColor.white)
        control.cornerRadius = 22
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.addTarget(self, action: #selector(confirm_handle), for: UIControl.Event.touchUpInside)
        return control
    }()
    
    
    fileprivate lazy var item0:ST_FilterFeedItemView = {
       let control = ST_FilterFeedItemView()
        control.model = ST_FilterFeedItemViewModel.init(str: "女生的日常", isSelected: false)
        return control
    }()
    
    fileprivate lazy var last_selected_item = item2
    
    fileprivate lazy var item1:ST_FilterFeedItemView = {
       let control = ST_FilterFeedItemView()
        control.model = ST_FilterFeedItemViewModel.init(str: "男生的日常", isSelected: false)
        return control
    }()
    
    fileprivate lazy var item2:ST_FilterFeedItemView = {
           let control = ST_FilterFeedItemView()
        
            control.model = ST_FilterFeedItemViewModel.init(str: "全部日常", isSelected: true)
            return control
        }()
    
    
    var confirm_closure:((ST_SEX)->())?
    
    @objc func confirm_handle(){
        
        if item0.model?.isSelected == true{
            
            confirm_closure?(ST_SEX.female)
        }
        var gender:ST_SEX
        
        switch self.last_selected_item {
        case item0:
            gender = ST_SEX.female
        case item1:
            gender = ST_SEX.male
        default:
            gender = ST_SEX.both
        }
        
        confirm_closure?(gender)
        
        goback_previous_controller()
    }
    
    @objc func close_handle(){
        goback_previous_controller()
    }
    
}

//MARK: - Model



fileprivate struct ST_FilterFeedItemViewModel {
    var str:String
    var isSelected:Bool
}


fileprivate class ST_FilterFeedItemView:UIView{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(textLabel)
        textLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(selectBtn)
        selectBtn.snp.makeConstraints { (make) in
            make.width.height.equalTo(50)
            make.right.equalToSuperview().offset(-11)
            make.centerY.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var textLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    
    fileprivate lazy var selectBtn:UIButton = {
        
        let control = UIButton.init(type: UIButton.ButtonType.custom)
       
        control.addTarget(self, action: #selector(click_handle), for: UIControl.Event.touchUpInside)
        control.setImage(ST_ImageUtils.imageName("right_icon_sel")!, for: UIControl.State.selected)
        control.setImage(ST_ImageUtils.imageName("right_icon_normal")!, for: UIControl.State.normal)
        
        return control
    }()

    var model:ST_FilterFeedItemViewModel?{
        didSet{
            
            refresh_bind_model()
        }
    }
    
    
    func refresh_bind_model(){
        guard let m = model else { return  }
        
        selectBtn.isSelected = m.isSelected
        
        textLabel.text = m.str
    }
    
    var click_closure:((ST_FilterFeedItemView,Bool)->())?
    
    @objc fileprivate func click_handle(){
        
        
        if self.selectBtn.isSelected{
            return
        }
        
        self.click_closure?(self,!self.selectBtn.isSelected)
        
    }
}




fileprivate class ST_FilterFeedSwitchItemView:UIView{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(textLabel)
        textLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(25)
            make.centerY.equalToSuperview()
        }
        
        self.addSubview(select_control)
        select_control.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-20)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var textLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    var text = ""{
        didSet{
            textLabel.text = text
        }
    }
    
    
    lazy var select_control:UISwitch = {
       let control = UISwitch()
         control.onTintColor = UIColor.init(hex: 0xFEC207)
         control.backgroundColor = UIColor.init(hex: 0xACB4C0)
         control.cornerRadius = 16
        return control
    }()
}
